﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Wyse.Server.Common
{
    /// <summary>
    /// Various utility functions
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// Converts a wildcard string to a regular expression
        /// </summary>
        /// <param name="wildcard">string containing wildcard characters (* and ?)</param>
        /// <param name="compile">true if the regex should be compiled, false otherwise</param>
        /// <returns>the created regex object</returns>
        public static Regex WildcardToRegex(String wildcard, bool compile)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append('^');
            sb.Append(Regex.Escape(wildcard).Replace("\\?", ".").Replace("\\*", ".*?"));
            sb.Append('$');

            RegexOptions ro = RegexOptions.IgnoreCase;
            if (compile)
                ro |= RegexOptions.Compiled;

            return new Regex(sb.ToString(), ro);
        }
    }
}
